package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class AddReaderActivity extends AppCompatActivity {
    private ImageView img;
    private EditText etName, etAge, etContact, etEmail, etCollege, etClass, etRemarks;
    private RadioGroup rgGender;
    private Button btnSubmit;
    private ReaderDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_add_reader);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        // 初始化数据库帮助类
        dbHelper = new ReaderDatabaseHelper(this);
        // 初始化视图
        img = findViewById(R.id.btn_back);
        etName = findViewById(R.id.et_name);
        etAge = findViewById(R.id.et_age);
        rgGender = findViewById(R.id.rg_gender);
        etContact = findViewById(R.id.et_contact);
        etEmail = findViewById(R.id.et_email);
        etCollege = findViewById(R.id.et_college);
        etClass = findViewById(R.id.et_class);
        etRemarks = findViewById(R.id.et_remarks);
        btnSubmit = findViewById(R.id.btn_submit);
        // 返回按钮
        img.setOnClickListener(v -> {
            finish();
        });
        // 提交按钮
        btnSubmit.setOnClickListener(v -> {
            saveReaderInfo();
        });
    }
    private void saveReaderInfo() {
        // 获取用户输入
        String name = etName.getText().toString().trim();
        String ageStr = etAge.getText().toString().trim();
        String contact = etContact.getText().toString().trim();
        String email = etEmail.getText().toString().trim();
        String college = etCollege.getText().toString().trim();
        String readerClass = etClass.getText().toString().trim();
        String remarks = etRemarks.getText().toString().trim();
        // 验证必填字段
        if (name.isEmpty()) {
            etName.setError("请输入姓名");
            etName.requestFocus();
            return;
        }
        // 解析年龄
        int age = 0;
        if (!ageStr.isEmpty()) {
            try {
                age = Integer.parseInt(ageStr);
            } catch (NumberFormatException e) {
                etAge.setError("请输入有效的年龄");
                etAge.requestFocus();
                return;
            }
        }
        // 获取性别
        String gender = "";
        int selectedId = rgGender.getCheckedRadioButtonId();
        if (selectedId == R.id.rb_male) {
            gender = "男";
        } else if (selectedId == R.id.rb_female) {
            gender = "女";
        } else {
            Toast.makeText(this, "请选择性别", Toast.LENGTH_SHORT).show();
            return;
        }
        // 保存到数据库
        long result = dbHelper.addReader(name, age, gender, contact, email, college, readerClass, remarks);
        if (result != -1) {
            Toast.makeText(this, "读者信息保存成功", Toast.LENGTH_SHORT).show();
            finish(); // 关闭当前Activity
        } else {
            Toast.makeText(this, "保存失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
}